package com.sinven.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
@Slf4j
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        log.debug("请求路径为：{} ",request.getRequestURI());
        //用户已登录，请求放行
        if (session != null && session.getAttribute("USER_SESSION")!=null){
            log.debug("用户已登录，当前登录用户未:{}",session.getAttribute("USER_SESSION"));
            return true;
        }
        log.debug("--- 用户未登录，地址已重置");
        response.sendRedirect("/manage/");
        return false;
    }
}
